<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author quqiang <quqiang@geek-zoo.com>
 *
 */

class Action extends admin_abs
{
	function index()
	{
	    // $count = _model(_t('admin'))->getCount();
	    // $pager = new Pager(20);
	    // $pager->generate($count);
	    // if ($pager->generate($count)) {
	    //      Smarty3::instance()->assign('pager',$pager);
	    // }
	    $data = _model(_t('admin'))->getList();
	    Smarty3::instance()->assign('data', $data);
	    Smarty3::instance()->display('user_list.html');
	}
	
	function create()
	{
	    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	        $info = _POST('info', array());
	        if ($info['password'] && $info['name']) {
	            $name = _model(_t('admin'))->read(array('name'=>$info['name']));
	            if (!$name) {
	                $info['password'] = md5($info['password']);
	                $permission = _POST('permission', array());
	                $t = array();
	                foreach ($permission as $key => $val) {
	                    if (!$val) {
	                       continue;
	                    }
                        $t[] = $key.':'.$val;
	                }
	                $info['permission'] = implode(',', $t);
        	        _model(_t('admin'))->create($info);
        	        redirect('?url=administrator');
	            } else {
	               msg('用户名存在');
	            }
	        } else {
	           msg('用户名和密码不能为空');
	        }
        } else {
    	    Smarty3::instance()->display('user_create.html');
        }
	}
	
	function update()
	{
	    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	        $info = _POST('info', array());
	        $id = _POST('id', 0);
	        if ($info['name']) {
	            if (!$name = _model(_t('admin'))->read("WHERE id<>$id AND name='{$info[name]}'")) {
	                if ($info['password']) {
    	                $info['password'] = md5($info['password']);
	                } else {
	                   unset($info['password']);
	                }

	                $permission = _POST('permission', array());
	                $t = array();
	                foreach ($permission as $key => $val) {
	                    if (!$val) {
	                       continue;
	                    }
                        $t[] = $key.':'.$val;
	                }
	                $info['permission'] = implode(',', $t);
	                
        	        _model(_t('admin'))->update(array('id'=>$id), $info);
        	        redirect('?url=web_user');
	            } else {
	               msg('用户名存在');
	            }
	        } else {
	           msg('用户名和密码不能为空');
	        }
	    } else {
	        $id = _GET('id', 0);
	        if ($id && $data = _model(_t('admin'))->read(array('id'=>$id))) {
        	    Smarty3::instance()->assign('data', $data);
        	    Smarty3::instance()->display('user_update.html');
	        }
	    }
	}
	
    function delete()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $id_array = array_filter(_POST('check', array()));

            foreach ($id_array as $key => $value) {
                if ($value == 1) {
                } else {
                    _model(_t('admin'))->delete(array('id'=>$value));
                }
            }
            redirect('?url=administrator');
        } else {
            $id = _GET('id', 0);
            if ($id == 1) {
                msg('创始人不可删除');
            }
            if ($id && $admin_info = _model(_t('admin'))->read(array('id'=>$id))) {
                _model(_t('admin'))->delete(array('id'=>$id));
                redirect('?url=administrator');
            }
        }
    }
}

?>